﻿using System;
using System.Collections.Generic;
using System.Linq;
using Xms.Abp.Data;

namespace Xms.Abp.Validation.StringValues;

public static class StringValueTypeExtensions
{
    public static DataType ToStringValueType(this Type type)
    {
        return type switch
        {
            Type t when (t == typeof(short) || t == typeof(ushort) || t == typeof(int) || t == typeof(uint) || t == typeof(long) || t == typeof(ulong) || t == typeof(byte)) => DataType.Integer,
            Type t when (t == typeof(float) || t == typeof(double) || t == typeof(decimal)) => DataType.Float,
            Type t when t == typeof(DateTime) => DataType.DateTime,
            Type t when t == typeof(TimeSpan) => DataType.Time,
            Type t when t == typeof(bool) => DataType.Boolean,
            Type t when t.Name == typeof(Dictionary<,>).Name => DataType.Dictionary,
            Type t when t.GetInterfaces().Any(r => r.Name == typeof(IEnumerable<>).Name || r.Name == typeof(ICollection<>).Name) => DataType.Array,
            _ => DataType.Text,
        };
    }
}
